package com.h1909d.weinfo.ui.fragment;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.h1909d.weinfo.R;
import com.h1909d.weinfo.base.BaseLazyFragment;
import com.h1909d.weinfo.bean.DailyNewsBean;
import com.h1909d.weinfo.presenter.DailyNewsPresenter;
import com.h1909d.weinfo.ui.activity.CalendarActivity;
import com.h1909d.weinfo.ui.activity.NewsDetailActivity;
import com.h1909d.weinfo.ui.adapter.RlvDailyNewsAdapter;
import com.h1909d.weinfo.util.LogUtil;
import com.h1909d.weinfo.util.TimeUtil;
import com.h1909d.weinfo.view.DailyNewsView;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.Calendar;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class DailyNewsFragment extends BaseLazyFragment<DailyNewsPresenter> implements DailyNewsView {

    @BindView(R.id.rlv)
    RecyclerView mRlv;
    @BindView(R.id.fab)
    FloatingActionButton mFab;
    private RlvDailyNewsAdapter rlvDailyNewsAdapter;

    public static DailyNewsFragment getInstance() {

        DailyNewsFragment fragment = new DailyNewsFragment();
        return fragment;
        // Required empty public constructor
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        mPresenter.getLastesNews();
    }

    @Override
    protected DailyNewsPresenter initPresenter() {
        return new DailyNewsPresenter();
    }

    @Override
    protected void initView(View view) {

        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        mRlv.setLayoutManager(new LinearLayoutManager(getContext()));
        ArrayList<DailyNewsBean.TopStoriesBean> bannerList = new ArrayList<>();
        ArrayList<DailyNewsBean.StoriesBean> newsList = new ArrayList<>();

        rlvDailyNewsAdapter = new RlvDailyNewsAdapter(getContext(), bannerList, newsList);
        mRlv.setAdapter(rlvDailyNewsAdapter);

        rlvDailyNewsAdapter.setOnItemClick(new RlvDailyNewsAdapter.onItemClick() {
            @Override
            public void dj(int i) {
                Intent intent = new Intent(getActivity(), NewsDetailActivity.class);
                intent.putExtra("id",rlvDailyNewsAdapter.mNewsList.get(i).getId());
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.fab)
    public void click(View v){
        switch (v.getId()){
            case R.id.fab:

                startActivity(new Intent(getActivity(), CalendarActivity.class));
                break;
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_daily_news;
    }

    @Override
    public void setData(DailyNewsBean dailyNewsBean) {
        rlvDailyNewsAdapter.setData(dailyNewsBean);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe()
    public void receiveDate(CalendarDay day){

        LogUtil.print("date:"+day.toString());

        //选中哪天就是哪天20200807
        String s = TimeUtil.parseTime(day.getCalendar());

/*        if (date是今天的日期){
            mPresenter.getLastesNews();
        }else {
            mPresenter.getOldNews();
        }*/

        //今天的日期,20200809
        Calendar instance = Calendar.getInstance();
        String tody = TimeUtil.parseTime(instance);

        if (tody.equals(s)){
            //选中的日期是今天
            mPresenter.getLastesNews();
        }else {
            //旧新闻给定20200807,后端返回的新闻是20200806的新闻,所以,日期需要+1
            Calendar selectCalendar = day.getCalendar();
            selectCalendar.add(Calendar.DAY_OF_MONTH,1);
            s = TimeUtil.parseTime(selectCalendar);
            mPresenter.getOldNews(s);
        }
    }
}